package info.javafans.cdn.struct;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class StructConfig {
	private static Map<String, ActionMapping> actionMappings = null;

	public StructConfig(String realPath) {
		if (actionMappings == null) {
			this.init(realPath);
		}
	}

	/**
	 * 初始化读取配置文件
	 * 
	 * @param realPath
	 */
	private void init(String realPath) {
		actionMappings = new HashMap<String, ActionMapping>();
		try {
//System.out.println("realPath:"+realPath);
			File f = new File(realPath + "/WEB-INF/struct-config.xml");
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			// 读取的配置文件
			Document doc = builder.parse(f);
			// action的list
			NodeList actionNodeList = doc.getElementsByTagName("action");

			// 遍历actionNodeList
			for (int i = 0; i < actionNodeList.getLength(); i++) {
				// 获得单个action
				Node singleAction = actionNodeList.item(i);
				// 获得action的属性
				NamedNodeMap actionAttributeMap = singleAction.getAttributes();
				Node actionPath = actionAttributeMap.getNamedItem("path");
				Node actionType = actionAttributeMap.getNamedItem("type");
//System.out.println("Path:"+actionPath+",type:"+actionType);
				// 创建ActionConfig对象
				ActionMapping actionConfig = new ActionMapping();
				actionConfig.setPath(actionPath.getNodeValue());// set path
				actionConfig.setType(actionType.getNodeValue());// set type

				// 读取forward节点
				NodeList forwardList = singleAction.getChildNodes();
				Map<String, String> forwardMap = new HashMap<String, String>();// Map
				for (int j = 1; j < forwardList.getLength(); j += 2) {

					Node firstForward = forwardList.item(j);

					// 读取forward节点的属性map
					NamedNodeMap forwardAttributeMap = firstForward.getAttributes();

					forwardMap.put(forwardAttributeMap.getNamedItem("name").getNodeValue(), forwardAttributeMap
							.getNamedItem("path").getNodeValue());
//System.out.println("forward: name:"+forwardAttributeMap.getNamedItem("name").getNodeValue()+",path:"+forwardAttributeMap
//							.getNamedItem("path").getNodeValue());
				}// end of for
				// 将forwardMap添加到actionConfig中
				actionConfig.setForwardMap(forwardMap);
				actionMappings.put(actionPath.getNodeValue(), actionConfig);
			}
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 按照action path获取
	 * 
	 * @param path
	 * @return
	 */
	public ActionMapping getActionMapping(String path) {
		Object o = actionMappings.get(path);
		return o == null ? null : (ActionMapping) o;
	}
}
